package com.sgl.myview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.sgl.myview.R;

/**
 * Created by XH on 2016/11/30.
 */
public class CustomProgressBar extends View {
    /**
     * 第一圈的颜色
     */
    private int mFirstColor;
    /**
     * 第二圈的颜色
     */
    private int mSecondColor;
    /**
     * 圈的宽度
     */
    private int mCircleWidth;
    /**
     * 画笔
     */
    private Paint mPaint;
    /**
     * 当前进度
     */
    private int mProgress;
    //开始进度
    private int mStartProgress = 270;

    /**
     * 速度
     */
    private int mSpeed;

    /**
     * 是否应该开始下一个
     */
    private boolean isNext = false;


    public CustomProgressBar(Context context) {
        this(context,null);
    }

    public CustomProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray mTypedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar,defStyleAttr,0);
        for(int i = 0 ; i < mTypedArray.getIndexCount() ; i++ ){
            int attr = mTypedArray.getIndex(i);
            switch (attr){
                case R.styleable.CustomProgressBar_firstColor:
                    mFirstColor = mTypedArray.getColor(attr, Color.BLUE);
                    break;
                case R.styleable.CustomProgressBar_secondColor:
                    mSecondColor = mTypedArray.getColor(attr,Color.RED);
                    break;
                case R.styleable.CustomProgressBar_circleWidth:
                    mCircleWidth = mTypedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,20,getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomProgressBar_speed:
                    mSpeed = mTypedArray.getInt(attr,20);
                    break;
            }
        }
        mTypedArray.recycle();
        mPaint = new Paint();
        new Thread(){
            @Override
            public void run() {
                while(true){
                    mProgress += 2;
                    if(mProgress == 330){
                        Log.e("qwer","mStartProgress---->" + mStartProgress);
                        Log.e("qwer","mProgress---->" + mProgress);
                        mStartProgress -= 30;
                        if(mStartProgress < 0){
                            mStartProgress += 360;
                        }
                        mProgress = 30;
                        isNext = !isNext;
                    }
                    postInvalidate();
                    try {
                        Thread.sleep(mSpeed);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    public void setSpeed(boolean status){
        if(status){
            mSpeed += 3;
        }else {
            if(mSpeed > 3) {
                mSpeed -= 3;
            }
        }
        postInvalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        int centre = getWidth() / 2;//获得圆心的x坐标
        int radius = centre - mCircleWidth / 2;//半径
        mPaint.setStrokeWidth(mCircleWidth);//// 设置圆环的宽度
        mPaint.setAntiAlias(true);// 消除锯齿
        mPaint.setStyle(Paint.Style.STROKE);// 设置空心
        mPaint.setStrokeCap(Paint.Cap.ROUND); // 定义线段断电形状为圆头
        mPaint.setAntiAlias(true); // 消除锯齿
        /**
         *  用于定义的圆弧的形状和大小的界限
         *  RectF (float left, float top, float right, float bottom)
         */
        RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius);
        if (!isNext) {// 第一颜色的圈完整，第二颜色跑
            //mPaint.setColor(mFirstColor); // 设置圆环的颜色
            //canvas.drawCircle(centre, centre, radius, mPaint); // 画出圆环
            mPaint.setColor(mSecondColor); // 设置圆环的颜色
            canvas.drawArc(oval, mStartProgress, mProgress, false, mPaint); // 根据进度画圆弧
        } else {
            //mPaint.setColor(mSecondColor); // 设置圆环的颜色
            //canvas.drawCircle(centre, centre, radius, mPaint); // 画出圆环
            mPaint.setColor(mSecondColor); // 设置圆环的颜色
            canvas.drawArc(oval, mStartProgress, mProgress - 360, false, mPaint); // 根据进度画圆弧
        }
    }
}
